using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Admin.NET.Application.DL645;
using Admin.NET.Application.Entity.Business;
using Admin.NET.Application.Enums;
using Admin.NET.Application.GuangHeMeter.ServerSocket;
using Admin.NET.Application.Util;
using Admin.NET.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Admin.NET.Application.Service.JobHttp;

/// <summary>
/// httpjob
/// </summary>
[ApiDescriptionSettings(new string[] { "业务应用" }, Order = 100)]
public class MeterReadOtherService : IDynamicApiController, ITransient, IPrivateDependency
{
	private readonly SqlSugarRepository<ElectricMeter> _rep;

	public MeterReadOtherService(SqlSugarRepository<ElectricMeter> rep)
	{
		_rep = rep;
	}

	/// <summary>
	/// 获取
	/// </summary>
	/// <returns></returns>
	[AllowAnonymous]
	[NonUnify]
	[HttpGet("/api/meter/read")]
	public async Task ReadOther()
	{
		List<ElectricMeter> eleterMeter = await (from u in _rep.AsQueryable()
			where u.SignalStatus == 1
			select u).ToListAsync();
		byte[] bytedatatrain = new byte[4] { 2, 107, 100, 100 };
		foreach (ElectricMeter item in eleterMeter)
		{
			(IAggrementPlugin, BrandModel) plugin = AggrementPluginUtil.GetPlugin(item);
			IAggrementPlugin pluginService = plugin.Item1;
			BrandModel currentModel = plugin.Item2;
			DL645Agreement dL645Agreement = new DL645Agreement();
			byte[] byteinfoyear = dL645Agreement.Send645Agreement(AgreementaType.Reading, item.LowAddr, "34333438").ToArray();
			string.Join("", from u in byteinfoyear.ToArray()
				select u.ToString("X2"));
			byte[] byteinfo = dL645Agreement.Send645Agreement(AgreementaType.Reading, item.LowAddr, "34343438").ToArray();
			string.Join("", from u in byteinfo.ToArray()
				select u.ToString("X2"));
			if (currentModel.Code == "JS376.1")
			{
				string[] result645 = ParseDLT645Result(await pluginService.Transform(valueCmd: string.Join("", (from u in BaseUtil.GetTransbyte(bytedatatrain, byteinfoyear, item.Sn)
					select u.ToString("x2")).ToArray()), imei: item.Sn));
				string result = ParseDLT645(string.Join("", result645));
				if (result == "0000000000")
				{
					break;
				}
				string format = "yyyyMMddHHmm";
				DateTime.Parse(provider: new DateTimeFormatInfo
				{
					ShortDatePattern = format
				}, s: "20" + result);
			}
		}
	}

	private string ParseDLT645(string body)
	{
		if (string.IsNullOrWhiteSpace(body))
		{
			return "";
		}
		List<byte> list = new List<byte>();
		list.Add(254);
		list.Add(254);
		list.Add(254);
		list.Add(254);
		bool sucess = true;
		for (int i = 0; i < body.Length / 2; i++)
		{
			byte byteInfo = (byte)Convert.ToInt32(body.Substring(i * 2, 2), 16);
			if (!(byteInfo == 254 && sucess))
			{
				sucess = false;
				list.Add(byteInfo);
			}
		}
		AgreementAnalysis resultInfo = DL645Agreement.DL645Analysis(list.ToArray());
		if (!resultInfo.Sucess)
		{
			throw Oops.Oh(ErrorCodeEnumEx.X1001);
		}
		if (!string.IsNullOrWhiteSpace(resultInfo.Data))
		{
			if (resultInfo.Data.Length > 8)
			{
				string infoType = resultInfo.Data.Substring(0, 8).ToUpper();
				string infoData = resultInfo.Data.Substring(8);
				return DL645ParameterSet.AnalisReading(infoType, infoData);
			}
			return "";
		}
		return "";
	}

	private string[] ParseDLT645Result(string body)
	{
		int index = 28;
		string typeCode = body.Substring(index, 8);
		index += 8;
		if (1 == 0)
		{
		}
		string[] result = ((!(typeCode == "00000100")) ? null : Get645Result(body, index));
		if (1 == 0)
		{
		}
		return result;
	}

	private string[] Get645Result(string body, int index)
	{
		index += 2;
		int length = Convert.ToInt32(body.Substring(index, 2), 16) * 2;
		index += 4;
		string result = body.Substring(index, length);
		string[] info = new string[length / 2];
		for (int i = 0; i < info.Length; i++)
		{
			info[i] = result.Substring(i * 2, 2);
		}
		return info;
	}
}
